package com.txunda.zhuohired.aty.user.resume;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ants.theantsgo.util.JSONUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.http.pst.ResumePst;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/7 16:54
 * 功能描述：自我评价
 * 联系方式：jiahuanchen@126.com
 */

public class SelfAppraisalAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 保存
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * 其他
     */
    @ViewInject(R.id.ll_self_appraisal_other)
    private LinearLayout ll_self_appraisal_other;
    /**
     * 其他人评价的标题
     */
    @ViewInject(R.id.tv_self_title)
    private TextView tv_self_title;
    /**
     * 其他人评价的内容
     */
    @ViewInject(R.id.tv_self_content)
    private TextView tv_self_content;

    /**
     * 评价内容
     */
    @ViewInject(R.id.et_self_appraisal)
    private EditText et_self_appraisal;
    /**
     * 字数
     */
    @ViewInject(R.id.tv_self_appraisal_num)
    private TextView tv_self_appraisal_num;

    private String self_appraisal = "";//自我评价
    private ResumePst resumePst;
    private String token = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("自我评价");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("保存");
    }

    @Override
    @OnClick({R.id.tv_self_appraisal_other, R.id.tv_self_appraisal_next, R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_self_appraisal_other://看看其他人怎么写
                ll_self_appraisal_other.setVisibility(View.VISIBLE);
                resumePst.getOtherSelf();
                break;
            case R.id.tv_self_appraisal_next://下一个
                ll_self_appraisal_other.setVisibility(View.VISIBLE);
                resumePst.getOtherSelf();
                break;
            case R.id.tv_layout_white_right://保存
                self_appraisal = et_self_appraisal.getText().toString().trim();
                if (TextUtils.isEmpty(self_appraisal)) {
                    showErrorTip("请输入自我评价");
                    return;
                }
                resumePst.self(token, self_appraisal);
                closeKeyboard(this, tv_layout_white_right);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_self_appraisal;
    }

    @Override
    protected void initialized() {
        resumePst = new ResumePst(this);
        token = application.getUserInfo().get("data");
    }

    @Override
    protected void requestData() {
        et_self_appraisal.addTextChangedListener(new myTextWatcher());//显示汉字个数监听
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("self")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            finish();
            return;
        }
        if(requestUrl.contains("getOtherSelf")){
            Map<String,String> map  =JSONUtils.parseKeyAndValueToMap(jsonStr);
            Map<String ,String > data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

            tv_self_title.setText(data.get("position"));

            tv_self_content.setText(data.get("self_speak"));



        }
    }

    /**
     * myTextWatcher实行TextWatcher
     * 接口并重写以下三种方法
     *
     * @author anynew
     */
    class myTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            //在文本改变前执行
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            //当文本正在改变时执行
        }

        @Override
        public void afterTextChanged(Editable s) {
            //统计汉字的个数
            //设置文本内容
            tv_self_appraisal_num.setText(s.length() + "");
            if (s.length() >= 200) {
                tv_self_appraisal_num.setTextColor(Color.RED);
            } else {
                tv_self_appraisal_num.setTextColor(Color.BLACK);
            }

        }
    }

}
